<%@page import="sun.awt.RepaintArea"%>
<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
request.setCharacterEncoding("utf-8");
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/warehouse";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "";
String sfz = null;
String zdz = null;
String rq = null;
String cplx = null;
String name = null;
String id = null;
sfz = request.getParameter("sfz");
zdz = request.getParameter("zdz");
rq = request.getParameter("time");
cplx = request.getParameter("cplx");
name = request.getParameter("name");
id=request.getParameter("id");
//遍历查询结果集
try {
    //加载驱动程序
    Class.forName(driver);
    //1.getConnection()方法，连接MySQL数据库！！
    con = DriverManager.getConnection(url,user,password);
    if(!con.isClosed())
        System.out.println("Succeeded connecting to the Database!");
    //2.创建statement类对象，用来执行SQL语句！！
    
    Statement statement =con.createStatement();
    //要执行的SQL语句
    
    if(sfz!=null){
    	String sqlz = "insert into cp(sf,zd,da,lx,na,nu) values ('" + sfz + "','" + zdz + "','" + rq + "','" + cplx + "','" + name + "','" + id + "')";
    	statement.executeUpdate(sqlz);
    }
    String sql = "select * from cp";
    //3.ResultSet类，用来存放获取的结果集！！
    ResultSet rs = statement.executeQuery(sql);
    
    System.out.println("执行结果如下所示:"); 
%>
<html>
<head>
	<style type="text/css">
	table{margin-top:80px;}
	</style>
</head>
<body background="1.jpg"
	style=" background-repeat:no-repeat ;
background-size:100% 100%; 
background-attachment: fixed;" >
<table border=1 align="center">
  <tr>
   <% out.println("<td>"+"出发地" + "</td><td>" + "终点站"+"</td><td>" +"日期"+"</td><td>" + "车票"+"</td><td>" + "姓名"+"</td><td>" + "证件号码"+"</td>");  %>
  </tr>

<%
    String sf = null;
    String zd = null;
    String da = null;
    String lx = null;
    String na = null;
    String nu = null;
    while(rs.next()){
        //获取stuname这列数据
        sf = rs.getString("sf");
        zd = rs.getString("zd");      
        da = rs.getString("da");
        lx = rs.getString("lx");
        na = rs.getString("na");
        nu = rs.getString("nu");
        //输出结果
%>
<head>
	<style type="text/css">
	table{margin-top:100px;}
	</style>
	</head>

	
		<tr>
		  <% out.println("<td>"+sf + "</td><td>" + zd +"</td><td>" + da +"</td><td>" + lx +"</td><td>" + na +"</td><td>" + nu +"</td>");%>
		</tr>
	</table >
<style>
	table tr td,table tr td{border:1px solid #0094ff;}	
	table{width:400px;min-height:50px;height:25px;text-align:center;bordr-collapse:collapse;}
</style>  
   </body>
   </html>
<% 
    }
    rs.close();
    con.close();
} catch(ClassNotFoundException e) {   
    //数据库驱动类异常处理
    out.println("Sorry,can`t find the Driver!");   
    e.printStackTrace();   
    } catch(SQLException e) {
    //数据库连接失败异常处理
    e.printStackTrace();  
    }catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}finally{
	System.out.println("数据库数据成功获取！！");
}
%>

